package com.caipin.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.caipin.dao.StudentCourseDao;
import com.caipin.dao.StudentDao;
import com.caipin.dao.StudentPhotoDao;
import com.caipin.domain.table.Student;
import com.caipin.domain.table.StudentCourse;
import com.caipin.domain.table.StudentPhoto;
import com.caipin.domain.web.PageBean;
import com.caipin.service.StudentCourseService;
import com.caipin.service.StudentManagementService;
import com.caipin.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: zhunian0322@163.com
 * @DateTime: 2020-08-01 16:20
 * @Description: TODO
 */
@Service
public class StudentManagementServiceImpl implements StudentManagementService {
    @Autowired
    private StudentService studentIService;

    @Autowired
    private StudentDao studentDao;

    @Autowired
    private StudentPhotoDao studentPhotoDao;

    @Autowired
    private StudentCourseService studentCourseIService;

    @Autowired
    private StudentCourseDao studentCourseDao;

    @Override
    public int importStudentToMysql(List<Student> studentList) {
        studentDao.truncateData();

        boolean b = studentIService.saveBatch(studentList);
        if (!b) {
            return 0;
        }

        return studentList.size();

    }

    @Override
    public PageBean<Student> findStudentListByStudentId(Integer pageNum, Integer pageSize, String studentId) {
        Page<Student> studentPage = new Page<>(pageNum, pageSize);
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", studentId);
        IPage<Student> page = studentDao.selectPage(studentPage, queryWrapper);

        PageBean<Student> pageBean = new PageBean<>();
        pageBean.setCurrPage(page.getCurrent());
        pageBean.setPageSize(page.getRecords().size());
        pageBean.setTotalPage(page.getPages());
        pageBean.setTotalCount(page.getTotal());
        pageBean.setList(page.getRecords());

        return pageBean;
    }

    @Override
    public PageBean<Student> findStudentList(Integer pageNum, Integer pageSize) {
        Page<Student> studentPage = new Page<>(pageNum, pageSize);

        IPage<Student> page = studentDao.selectPage(studentPage, null);

        PageBean<Student> pageBean = new PageBean<>();
        pageBean.setCurrPage(page.getCurrent());
        pageBean.setPageSize(page.getRecords().size());
        pageBean.setTotalPage(page.getPages());
        pageBean.setTotalCount(page.getTotal());
        pageBean.setList(page.getRecords());

        return pageBean;
    }

    @Override
    public PageBean<Student> findStudentList(Integer pageNum, Integer pageSize, String studentName, String gender, String grade, String clbum) {
        Page<Student> studentPage = new Page<>(pageNum, pageSize);


        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();

        if (studentName != null) {
            queryWrapper.eq("name", studentName);
        }

        if (gender != null) {
            queryWrapper.eq("gender", gender);
        }

        if (grade != null) {
            queryWrapper.eq("grade", grade);
        }

        if (clbum != null) {
            queryWrapper.eq("clbum", clbum);
        }


        IPage<Student> page = studentDao.selectPage(studentPage, queryWrapper);

        PageBean<Student> pageBean = new PageBean<>();
        pageBean.setCurrPage(page.getCurrent());
        pageBean.setPageSize(page.getRecords().size());
        pageBean.setTotalPage(page.getPages());
        pageBean.setTotalCount(page.getTotal());
        pageBean.setList(page.getRecords());

        return pageBean;
    }

    @Override
    public Student findStudentByStudentId(String studentId) {
        return studentDao.selectById(studentId);
    }

    @Override
    public void delStudentByStudentId(String studentId) {
        studentDao.deleteById(studentId);
    }

    @Override
    public int delStudentListByStudentIds(List<String> studentIds) {
        return studentDao.deleteBatchIds(studentIds);
    }

    @Override
    public void addStudent(Student student) {
        studentDao.insert(student);
    }

    @Override
    public void updateStudent(Student student) {
        studentDao.updateById(student);
    }

    @Override
    @Transactional
    public void saveStudentPhoto(StudentPhoto studentPhoto) {
        studentPhotoDao.delete(new QueryWrapper<StudentPhoto>().eq("student_id", studentPhoto.getStudentId()));
        studentPhotoDao.insert(studentPhoto);
    }

    @Override
    public int importStudentCourseToMysql(List<StudentCourse> studentCourseList) {
        studentCourseDao.truncateData();

        boolean b = studentCourseIService.saveBatch(studentCourseList);
        if (!b) {
            return 0;
        }

        return studentCourseList.size();
    }

    @Override
    public PageBean<StudentCourse> findStudentCourseListByStudentId(Integer pageNum, Integer pageSize, String studentId) {
        Page<StudentCourse> studentCoursePage = new Page<>(pageNum, pageSize);

        QueryWrapper<StudentCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("student_id", studentId);

        IPage<StudentCourse> page = studentCourseDao.selectPage(studentCoursePage, queryWrapper);

        PageBean<StudentCourse> pageBean = new PageBean<>();
        pageBean.setCurrPage(page.getCurrent());
        pageBean.setPageSize(page.getRecords().size());
        pageBean.setTotalPage(page.getPages());
        pageBean.setTotalCount(page.getTotal());
        pageBean.setList(page.getRecords());

        return pageBean;
    }

    @Override
    public PageBean<StudentCourse> findStudentCourseList(Integer pageNum, Integer pageSize) {
        Page<StudentCourse> studentCoursePage = new Page<>(pageNum, pageSize);


        IPage<StudentCourse> page = studentCourseDao.selectPage(studentCoursePage, null);

        PageBean<StudentCourse> pageBean = new PageBean<>();
        pageBean.setCurrPage(page.getCurrent());
        pageBean.setPageSize(page.getRecords().size());
        pageBean.setTotalPage(page.getPages());
        pageBean.setTotalCount(page.getTotal());
        pageBean.setList(page.getRecords());

        return pageBean;
    }

    @Override
    public PageBean<StudentCourse> findStudentCourseList(Integer pageNum, Integer pageSize, String courseName, String courseType, String building, String classroom, String schoolYear, Integer semester, String grade, String clbum, Integer week, Integer sections) {
        Page<StudentCourse> studentCoursePage = new Page<>(pageNum, pageSize);

        QueryWrapper<StudentCourse> queryWrapper = new QueryWrapper<>();
        if (courseName != null) {
            queryWrapper.eq("course_name", courseName);
        }

        if (courseType != null) {
            queryWrapper.eq("course_type", courseType);
        }

        if (building != null) {
            queryWrapper.eq("building", building);
        }

        if (classroom != null) {
            queryWrapper.eq("classroom", classroom);
        }

        if (schoolYear != null) {
            queryWrapper.eq("school_year", schoolYear);
        }

        if (semester != null) {
            queryWrapper.eq("semester", semester);
        }

        if (grade != null) {
            queryWrapper.eq("grade", grade);
        }

        if (clbum != null) {
            queryWrapper.eq("clbum", clbum);
        }

        if (week != null) {
            queryWrapper.eq("week", week);
        }

        if (sections != null) {
            queryWrapper.eq("sections", sections);
        }

        IPage<StudentCourse> page = studentCourseDao.selectPage(studentCoursePage, queryWrapper);

        PageBean<StudentCourse> pageBean = new PageBean<>();
        pageBean.setCurrPage(page.getCurrent());
        pageBean.setPageSize(page.getRecords().size());
        pageBean.setTotalPage(page.getPages());
        pageBean.setTotalCount(page.getTotal());
        pageBean.setList(page.getRecords());

        return pageBean;
    }

    @Override
    public StudentCourse findStudentCourseByStudentCourseId(Long studentCourseId) {
        StudentCourse studentCourse = studentCourseDao.selectById(studentCourseId);
        return studentCourse;
    }

    @Override
    public void delStudentCourseByStudentCourseId(Long studentCourseId) {
        studentCourseDao.deleteById(studentCourseId);
    }

    @Override
    public int delStudentCourseListByStudentCourseIds(List<Long> studentCourseIds) {
        int i = studentCourseDao.deleteBatchIds(studentCourseIds);
        return i;
    }

    @Override
    public void addStudentCourse(StudentCourse studentCourse) {
        studentCourseDao.insert(studentCourse);
    }

    @Override
    public void updateStudentCourse(StudentCourse studentCourse) {
        studentCourseDao.updateById(studentCourse);
    }

    @Override
    public Integer studentCourseCount(String courseName, String grade, String clbum, Integer week, Integer sections) {

        QueryWrapper<StudentCourse> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("course_name", courseName)
                .eq("grade", grade)
                .eq("clbum", clbum)
                .eq("week", week)
                .eq("sections", sections);

        Integer count = studentCourseDao.selectCount(queryWrapper);

        return count;
    }

    @Override
    public Integer studentCourseCountByClassStudent(String grade, String clbum) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("grade", grade)
                .eq("clbum", clbum);


        Integer count = studentDao.selectCount(queryWrapper);

        return count;
    }


}
